package com.tarena.lbs.coupon.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.api.pojo.param.UserCouponsParam;
import com.tarena.lbs.coupon.enums.StockResultEnum;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.query.CouponQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponCodeQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import com.tarena.lbs.coupon.service.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * 处理和优惠券数据业务有关的接口
 */
@RestController
@Slf4j
@Tag(name="优惠券模块")
public class CouponController {

    @Autowired
    private CouponService couponService;


    @Operation(summary = "查询当前商家优惠券分页列表")
    @PostMapping("/admin/marketing/coupon/info/list")
    public Result<PageResult<CouponVO>> pageList(@RequestBody CouponQuery query) throws BusinessException{
        log.info("查询当前商家优惠券分页列表入参:{}",query);
        PageResult<CouponVO> voPage = couponService.pageList(query);
        return new Result<>(voPage);
    }


    @Operation(summary = "新增优惠券")
    @PostMapping("/admin/marketing/coupon/info/save")
    public Result<Void> addCoupon(@RequestBody CouponParam couponParam) throws BusinessException, ParseException {
        log.info("新增优惠券入参:{}",couponParam);
        if (couponParam == null) throw new BusinessException(StockResultEnum.DATA_NOT_FIND);
        couponService.updateCoupon(couponParam);
        return Result.success();
    }

    @Operation(summary = "修改优惠券")
    @PostMapping("/admin/marketing/coupon/info/update")
    public Result<Void> updateCoupon(@RequestBody CouponParam couponParam) throws BusinessException, ParseException {
        log.info("修改优惠券入参:{}",couponParam);
        if (couponParam == null) throw new BusinessException(StockResultEnum.DATA_NOT_FIND);
        couponService.updateCoupon(couponParam);
        return Result.success();
    }

    @Operation(summary = "查询优惠券详情")
    @GetMapping("/admin/marketing/coupon/info/detail/{id}")
    public Result<CouponVO> detail(@PathVariable("id") Integer id)throws BusinessException{
        log.info("查询优惠券详情入参:{}",id);
        CouponVO couponVO = couponService.detail(id);
        log.info("查询优惠券详情成功，出参:{}",couponVO);
        return new Result<>(couponVO);
    }



    @Operation(summary = "领取优惠券")
    @PostMapping("/marketing/user/receive/save")
    public Result<Void> receiveCoupon(@RequestBody UserCouponsParam param)throws BusinessException{
        return null;
    }


    @Operation(summary = "用户领取优惠券列表")
    @PostMapping("/marketing/user/receive/list")
    public Result<PageResult<UserCouponsVO>> receiveList(@RequestBody UserCouponQuery couponQuery)throws BusinessException{
        return null;
    }


    @Operation(summary = "查看优惠券码的二维码图片")
    @PostMapping("/marketing/user/receive/detail")
    public Result<UserCouponsVO> receiveDetail(@RequestBody UserCouponCodeQuery query)
        throws BusinessException{
        return null;
    }

    @Operation(summary = "删除优惠券")
    @PostMapping("/admin/marketing/coupon/info/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Integer id)throws BusinessException{
        log.info("删除优惠券入参:{}",id);
        if(id == null) throw new BusinessException(StockResultEnum.DATA_NOT_FIND);
        couponService.delete(id);
        return Result.success();
    }

    @Operation(summary = "优惠券启用状态")
    @PostMapping("/admin/marketing/coupon/info/updateStatus")
    public Result<Void> updateStatus(@RequestBody CouponParam couponParam)throws BusinessException{
        log.info("优惠券启用状态入参:{}",couponParam);
        if (couponParam == null) throw new BusinessException(StockResultEnum.DATA_NOT_FIND);
        couponService.updateStatus(couponParam.getId(),couponParam.getEnableStatus());
        return Result.success();
    }


}
